<?php

/**
 * @copyright 2011
 * @version $Id$
 * @access public
 * @author oriotiori

 */

class Admin_Model_Products extends Zend_Db_Table_Abstract{

	protected $_name = 'products';

	/**
	 * Todas las imagenes de un producto
	 * @param int $id
	 * @return array $data
	 */
	public function getAllImagesByProduct($id=0)
	{
		$TO = new Admin_Model_TO_Products();
		$data=array();
		try{
			$sql=$this->getAdapter()
			->select()
			->from(
			array('pi' => TABLE_PREFIX.'images_product'),
			array('pi.Id_Image', 'pi.Id_Product')
			)
			->joinLeft(	array('i' => TABLE_PREFIX.'images'), "i.Id=pi.Id_Image",
			array('i.Thumb', 'i.Medium', 'i.Mini_Thumb', 'i.Real')
			)
			->where("pi.Id_Product=?", $id);

			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);

			if($rowCount>0){
				foreach($res as $dt){
					$TO->setIdImage($dt['Id_Image']);
					$TO->setIdProduct($dt['Id_Product']);
					$TO->setThumb($dt['Thumb']);
					$TO->setMedium($dt['Medium']);
					$TO->setReal($dt['Real']);
					$TO->setMiniThumb($dt['Mini_Thumb']);
					$TO->add();
				}
				$data = $TO->getAllData();
				return $data;
			}
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}


	/**
	 * get Product
	 * @param int $id
	 * @return array $data
	 */
	public function getProductsByCategory($id=0, $status='all')
	{
		$TO = new Admin_Model_TO_Products();
		$facadeCategories= new Admin_Model_Facade_Categories();

		$data=array();
		try{
			$sql=$this->getAdapter()
			->select()
			->from(
			array('p' => TABLE_PREFIX.$this->_name),
			array('p.Id', 'p.Name', 'p.Id_Category', 'p.Status', 'p.Description')
			)
			->joinLeft(	array('pa' => TABLE_PREFIX.'products_attributes'),
                        			'p.Id=pa.Product_Id', 
			array('pa.Product_Price', 'pa.Product_Model', 'pa.Product_Quantity')
			)
			->joinLeft(	array('m' => TABLE_PREFIX.'manufacturers'),
    					'm.Id=p.Id_Manufacturer',
			array('m.Manufacturer')
			)
			->joinLeft(	array('t' => TABLE_PREFIX.'taxes'),
    					't.Id=pa.Product_Tax_Class_Id',
			array('Product_Tax_Name' => 't.Name', 'Product_Tax_Percent' => 't.Percent', 'Id_Tax' => 't.Id')
			)
			->where("p.Id_Category=?", $id)
			->order('p.Id DESC');

			if($status==1){
				$sql=$sql->where('p.Status=?', 1);
			}

			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);

			if($rowCount>0){
				foreach($res as $dt){
					$TO->setId($dt['Id']);
					$TO->setType('Product');
					$TO->setName($dt['Name']);
					$TO->setManufacturer($dt['Manufacturer']);
					$TO->setIdCategory($dt['Id_Category']);
					$TO->setStatus($dt['Status']);
					$TO->setPrice($dt['Product_Price']);
					$TO->setTaxName($dt['Product_Tax_Name']);
					$TO->setTaxPercent($dt['Product_Tax_Percent']);
					$TO->setTaxId($dt['Id_Tax']);

					$TO->setDescription($dt['Description']);
					$TO->setModel($dt['Product_Model']);
					$TO->setCantidad($dt['Product_Quantity']);
					$TO->setAllImagesByProduct($this->getAllImagesByProduct($dt['Id']));
					$TO->setTreeCategory($facadeCategories->getTreeCatById($dt['Id_Category']));
					$TO->add();
				}
				$data = $TO->getAllData();
				return $data;
			}
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}


	/**
	 * get Product
	 * @return array $data
	 */
	public function getProductsExcludeFeatured($status='all')
	{
		$TO = new Admin_Model_TO_Products();
		$facadeCategories= new Admin_Model_Facade_Categories();

		$data=array();
		try{
			$sql=$this->getAdapter()
			->select()
			->from(
			array('p' => TABLE_PREFIX.$this->_name),
			array('p.Id', 'p.Name')
			)
			->joinLeft(	array('pa' => TABLE_PREFIX.'products_attributes'),
                        'p.Id=pa.Product_Id', 
			array('pa.Product_Price', 'pa.Product_Model')
			)
			->joinLeft(	array('c' => TABLE_PREFIX.'categories'),
    					'p.Id_Category=c.Id',
			array('c.Category')
			)
			->joinLeft(	array('t' => TABLE_PREFIX.'taxes'),
    					't.Id=pa.Product_Tax_Class_Id',
			array('Product_Tax_Percent' => 't.Percent')
			)
				
			->joinLeft(	array('f' => TABLE_PREFIX.'products_featured'),
			    			'f.Product_Id=p.Id',
			array('Product_Featured_Id' => 'f.Id')
			)
			->where('f.Product_Id '. new Zend_Db_Expr('IS NULL'))
			->order('p.Id DESC');
				
			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);

			if($rowCount>0){
				foreach($res as $dt){
					$TO->setId($dt['Id']);
					$TO->setName($dt['Name']);
					$TO->setCategory($dt['Category']);
					$TO->setPrice($dt['Product_Price']);
					$TO->setTaxPercent($dt['Product_Tax_Percent']);

					$TO->setModel($dt['Product_Model']);
					$TO->add();
				}
				$data = $TO->getAllData();
				return $data;
			}
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}


	/**
	 * get Products
	 * @return array $data
	 */
	public function getFeatured($id=0, $status='all')
	{
		$TO = new Admin_Model_TO_Products();
		$facadeCategories= new Admin_Model_Facade_Categories();
			
		$data=array();
		try{
			$sql=$this->getAdapter()->select()
			->from(
			array('f' => TABLE_PREFIX.'products_featured'),
			array('f.Id', 'f.Start_Date', 'f.End_Date')
			)
			->joinLeft(
			array('p' => TABLE_PREFIX.$this->_name),
				'f.Product_Id=p.Id',
			array('p.Id', 'p.Name', 'p.Id_Category', 'p.Status', 'p.Description')
			)
			->joinLeft(	array('pa' => TABLE_PREFIX.'products_attributes'),
		    	        'p.Id=pa.Product_Id', 
			array('pa.Product_Price', 'pa.Product_Model', 'pa.Product_Quantity')
			)
			->joinLeft(	array('m' => TABLE_PREFIX.'manufacturers'),
			    		'm.Id=p.Id_Manufacturer',
			array('m.Manufacturer')
			)
			->joinLeft(array('t' => TABLE_PREFIX.'taxes'),
			    		't.Id=pa.Product_Tax_Class_Id',
			array('Product_Tax_Name' => 't.Name', 'Product_Tax_Percent' => 't.Percent', 'Id_Tax' => 't.Id')
			)
			->order('p.Id DESC');


			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);

			if($rowCount>0){
				foreach($res as $dt){
					$TO->setId($dt['Id']);
					$TO->setType('Product');
					$TO->setStartDate($dt['Start_Date']);
					$TO->setEndDate($dt['End_Date']);
					$TO->setName($dt['Name']);
					$TO->setManufacturer($dt['Manufacturer']);
					$TO->setIdCategory($dt['Id_Category']);
					$TO->setStatus($dt['Status']);
					$TO->setPrice($dt['Product_Price']);
					$TO->setTaxName($dt['Product_Tax_Name']);
					$TO->setTaxPercent($dt['Product_Tax_Percent']);
					$TO->setTaxId($dt['Id_Tax']);
					$TO->setDescription($dt['Description']);
					$TO->setModel($dt['Product_Model']);
					$TO->setCantidad($dt['Product_Quantity']);
					$TO->setAllImagesByProduct($this->getAllImagesByProduct($dt['Id']));
					$TO->setTreeCategory($facadeCategories->getTreeCatById($dt['Id_Category']));
					$TO->add();
				}
				$data = $TO->getAllData();
				return $data;
			}
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}

	/**
	 * get TotalProducts
	 * @return array $data
	 */
	public function getTotalProducts()
	{
		$TO = new Admin_Model_TO_Products();

		$data=array();
		try{
			$sql=$this->getAdapter()
			->select()
			->from(
			array('p' => TABLE_PREFIX.$this->_name),
			array('Totalproducts' => new Zend_Db_Expr("COUNT('p.Id')") )
			)
			->where('p.Status=?', 1)
			->where("1");

			$res = $this->getAdapter()->fetchAll($sql);

			foreach($res as $dt){
				$TO->setTotalproducts($dt['Totalproducts']);
			}
			$data = $TO->getAllVars();
			return $data;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}



	/**
	 * Obtiene productos data
	 * @param int $id
	 * @return array $data
	 */
	public function getProductById($id=0)
	{
		$TO = new Admin_Model_TO_Products();
		$data=array();
		
		try{
			$sql=$this->getAdapter()
			->select()
			->from(
			array('p' => TABLE_PREFIX.$this->_name),
			array(	'p.Id', 'p.Status', 'p.Name',
                 	'p.Shortlink', 'p.Description', 'p.Clicked',
                    'p.Sold', 'p.Id_Category', 'p.Id_Manufacturer' , 'p.Tags'
			)
			)
			->joinLeft(	array('pa' => TABLE_PREFIX.'products_attributes'),
                    			'p.Id=pa.Product_Id', 
			array('pa.Product_Date_Avaliable', 'pa.Product_Tax_Class_Id', 'pa.Product_Price',
                  'pa.Product_Virtual', 'pa.Product_Is_Always_Free_Shipping', 'pa.Product_Qty_Box_Status',
                  'pa.Product_Quantity_Order_Min', 'pa.Product_Quantity_Order_Max', 'pa.Product_Quantity_Order_Units',
                  'pa.Product_Quantity', 'pa.Product_Model', 'pa.Product_Url',
                  'pa.Product_Weight', 'pa.Product_Size', 'pa.Product_Datasheet' 
			)
			)
			->joinLeft(	array('t' => TABLE_PREFIX.'taxes'),
		                        't.Id=pa.Product_Tax_Class_Id', 
			array('Product_Tax_Name' => 't.Name', 'Product_Tax_Percent' => 't.Percent', 'Id_Tax' => 't.Id')
			)
			->joinLeft(array('c' => TABLE_PREFIX.'categories'),
					           'c.Id=p.Id_Category',
			array('c.Category')
			)
			->where("p.Id=?", $id)
			->order('p.Id DESC');

			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);

			if($rowCount>0){
				foreach($res as $dt){
					$TO->setId($dt['Id']);
					$TO->setProduct_Status($dt['Status']);
					$TO->setProduct_Name($dt['Name']);
					$TO->setProduct_Category($dt['Category']);
					$TO->setProduct_Shortlink($dt['Shortlink']);
					$TO->setProduct_Description($dt['Description']);
					$TO->setProduct_Category_Id($dt['Id_Category']);
					$TO->setProduct_Sold($dt['Sold']);
					$TO->setProduct_Tags($dt['Tags']);
					$TO->setProduct_Clicked($dt['Clicked']);
					$TO->setProduct_Manufacturer_Id($dt['Id_Manufacturer']);
					$TO->setProduct_Date_Avaliable($dt['Product_Date_Avaliable']);
					$TO->setProduct_Tax_Class_Id($dt['Product_Tax_Class_Id']);
					$TO->setProduct_Price($dt['Product_Price']);

					$TO->setTaxName($dt['Product_Tax_Name']);
					$TO->setTaxPercent($dt['Product_Tax_Percent']);
					$TO->setTaxId($dt['Id_Tax']);

					$TO->setProduct_Virtual($dt['Product_Virtual']);
					$TO->setProduct_Is_Always_Free_Shipping($dt['Product_Is_Always_Free_Shipping']);
					$TO->setProduct_Qty_Box_Status($dt['Product_Qty_Box_Status']);
					$TO->setProduct_Quantity_Order_Min($dt['Product_Quantity_Order_Min']);
					$TO->setProduct_Quantity_Order_Max($dt['Product_Quantity_Order_Max']);
					$TO->setProduct_Quantity_Order_Units($dt['Product_Quantity_Order_Units']);
					$TO->setProduct_Quantity($dt['Product_Quantity']);
					$TO->setProduct_Model($dt['Product_Model']);
					$TO->setProduct_Url($dt['Product_Url']);
					$TO->setProduct_Weight($dt['Product_Weight']);
					$TO->setProduct_Size($dt['Product_Size']);
					$TO->setProduct_Datasheet($dt['Product_Datasheet']);

					$TO->setAllImagesByProduct($this->getAllImagesByProduct($dt['Id']));

				}
				$data = $TO->getAllVars();
				return $data;
			}
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}



	/**
	 * Agrega nuevo producto
	 * @param Array $to
	 * @return int $lastId
	 */
	public function addProduct($to)
	{
		try{
			$data = array(
                  	'Name'     			=> $to['ProductName'],
                  	'Description'  		=> (!empty($to['ProductDescription'])) ? $to['ProductDescription'] : new Zend_Db_Expr('null'),
                  	'Id_Category'  		=> $to['ProductCategoryId'],
                  	'Id_Manufacturer'	=> $to['ProductManufacturerId'],
                  	'Tags'				=> $to['ProductTags'],
            		'Status'  			=> $to['ProductStatus'],
            		'Creation_Date'		=> new Zend_Db_Expr('now()')
			);

			$res = $this->getAdapter()->insert(TABLE_PREFIX.$this->_name ,$data);
			$lastId= $this->getAdapter()->lastInsertId();
			if($lastId > 0){
				$to['ProductId']=$lastId;
				$this->addProductAttributesByIdProduct($to);
				return $lastId;
			}
			return false;

		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}



	/**
	 * Agrega nuevo producto
	 * @param Array $to
	 * @return int $lastId
	 */
	public function addProductFeatured($to)
	{
		try{
			$data = array(
                  	'Product_Id'    => $to['ProductId'],
            		'Start_Date'	=> $to['StartDate'],
            		'End_Date'		=> $to['EndDate']
			);

			$res = $this->getAdapter()->insert(TABLE_PREFIX.'products_featured' ,$data);
			$lastId= $this->getAdapter()->lastInsertId();
			return $lastId;

		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}

	/**
	 * Actualiza Producto
	 * @param Array $to
	 * @return int $lastId
	 */
	public function updateProductById($to)
	{
			
		try{
			$data = array(
                  	'Name'     			=> $to['ProductName'],
                  	'Description'  		=> (!empty($to['ProductDescription'])) ? $to['ProductDescription'] : new Zend_Db_Expr('null'),
                  	'Id_Category'  		=> $to['ProductCategoryId'],
                  	'Id_Manufacturer'	=> $to['ProductManufacturerId'],
            		'Tags'				=> $to['ProductTags'],
            		'Status'  			=> $to['ProductStatus']
			);
			$where[]="Id=". $to['ProductId'];
			$res = $this->getAdapter()->update(TABLE_PREFIX.$this->_name ,$data, $where);

			$this->updateProductAttributesByIdProduct($to);
			return true;

		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}



	/**
	 * atributos del producto
	 * @param Array $to
	 * @return int $lastId
	 */
	public function addProductAttributesByIdProduct($to)
	{
		try{


			$data = array(
                  	'Product_Id'     					=> $to['ProductId'],
                  	'Product_Date_Avaliable'     		=> (!empty($to['ProductDateAvaliable'])) ? $to['ProductDateAvaliable'] : date("Y-m-d"),
            		'Product_Tax_Class_Id'     			=> $to['ProductTaxClassId'],
            		'Product_Price'  					=> number_format($to['ProductPrice'], 2, '.', ''),
                  	'Product_Virtual'					=> $to['ProductVirtual'],
            		'Product_Is_Always_Free_Shipping'	=> $to['ProductIsAlwaysFreeShipping'],
            		'Product_Qty_Box_Status'			=> $to['ProductsQtyBoxStatus'],
            		'Product_Quantity_Order_Min'		=> $to['ProductQuantityOrderMin'],
            		'Product_Quantity_Order_Max'		=> $to['ProductQuantityOrderMax'],
            		'Product_Quantity_Order_Units'		=> $to['ProductQuantityOrderUnits'],
                    'Product_Quantity'					=> (!empty($to['ProductQuantity'])) ? $to['ProductQuantity'] : 0,
                    'Product_Model'						=> (!empty($to['ProductModel'])) ? $to['ProductModel'] : new Zend_Db_Expr('null'),
                    'Product_Url'						=> (!empty($to['ProductUrl'])) ? $to['ProductUrl'] : new Zend_Db_Expr('null'),
                    'Product_Weight'					=> (!empty($to['ProductWeight'])) ? $to['ProductWeight'] : 0,
					'Product_Size'						=> (!empty($to['ProductSize'])) ? $to['ProductSize'] : new Zend_Db_Expr('null'),
            		'Product_Datasheet'					=> (!empty($to['ProductDatasheet'])) ? $to['ProductDatasheet'] : new Zend_Db_Expr('null')

			);


			$res = $this->getAdapter()->insert(TABLE_PREFIX.'products_attributes' ,$data);
			$lastId= $this->getAdapter()->lastInsertId();
			return $lastId;

		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}


	/**
	 * atributos del producto
	 * @param Array $to
	 * @return int $lastId
	 */
	public function updateProductAttributesByIdProduct($to)
	{
		try{

			$sql=$this->getAdapter()
			->select()
			->from(
			array('a' => TABLE_PREFIX.'products_attributes'),
			array('a.Product_Id')
			)
			->where("a.Product_Id=".$to['ProductId']);

			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);

			if($rowCount == 0){
				$this->addProductAttributesByIdProduct($to);
			}
			else{
				$data = array(
	                  	'Product_Date_Avaliable'     		=> $to['ProductDateAvaliable'],
	            		'Product_Tax_Class_Id'     			=> $to['ProductTaxClassId'],
	            		'Product_Price'  					=> number_format($to['ProductPrice'], 2, '.', ''),
	                  	'Product_Virtual'					=> $to['ProductVirtual'],
	            		'Product_Is_Always_Free_Shipping'	=> $to['ProductIsAlwaysFreeShipping'],
	            		'Product_Qty_Box_Status'			=> $to['ProductsQtyBoxStatus'],
	            		'Product_Quantity_Order_Min'		=> $to['ProductQuantityOrderMin'],
	            		'Product_Quantity_Order_Max'		=> $to['ProductQuantityOrderMax'],
	            		'Product_Quantity_Order_Units'		=> $to['ProductQuantityOrderUnits'],
	            		'Product_Quantity'					=> (!empty($to['ProductQuantity'])) ? $to['ProductQuantity'] : 0,
                        'Product_Model'						=> (!empty($to['ProductModel'])) ? $to['ProductModel'] : new Zend_Db_Expr('null'),
                        'Product_Url'						=> (!empty($to['ProductUrl'])) ? $to['ProductUrl'] : new Zend_Db_Expr('null'),
                        'Product_Weight'					=> (!empty($to['ProductWeight'])) ? $to['ProductWeight'] : 0,
            			'Product_Size'						=> (!empty($to['ProductSize'])) ? $to['ProductSize'] : new Zend_Db_Expr('null'),
                        'Product_Datasheet'					=> (!empty($to['ProductDatasheet'])) ? $to['ProductDatasheet'] : new Zend_Db_Expr('null')

				);
				$where="Product_Id=".$to['ProductId'];
				$res = $this->getAdapter()->update(TABLE_PREFIX.'products_attributes' ,$data, $where);
			}
			return true;

		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}



	/**
	 * @param Array $to
	 * @return boolean
	 */
	public function AddImageDB($to)
	{
		try{
			$data = array(
			    'Id_Image'  => (int)$to['IdPicture']
			);
			$where[]="Id=".$to['IdProduct'];
			$res = $this->getAdapter()->update(TABLE_PREFIX.$this->_name ,$data, $where);
			return $res;

		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}


	/**
	 * @param int id
	 * @return boolean
	 */
	public function deleteImageProduct($id=0)
	{
			
		try{
			$facadeImage= new Admin_Model_Facade_ImageHandler();

			$dataImage=$facadeImage->getImageById($id);
			$res = $this->getAdapter()->delete(TABLE_PREFIX.'images_product' ,"Id_Image=".$id);
			$facadeImage->deleteImage($id);
			$path=array();

			$path[0]=IMAGES_PATH.DS."product".DS."Real".DS.$dataImage['Real'];
			$path[1]=IMAGES_PATH.DS."product".DS."Medium".DS.$dataImage['Medium'];
			$path[2]=IMAGES_PATH.DS."product".DS."Thumb".DS.$dataImage['Real'];
			$path[3]=IMAGES_PATH.DS."product".DS."Mini_Thumb".DS.$dataImage['MiniThumb'];

			$facadeImage->deleteImageByPath($path);


			return $res;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}


	/**
	 * Crea Thumb
	 * @param Array $to
	 * @return boolean
	 */
	public function CreateThumb($to)
	{
		try{
			$imageHandler= new Admin_Model_ImageHandler();
			$imageHandler->CreateThumb($to);
			return true;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}


	/**
	 * @param int id
	 * @return boolean
	 */
	public function deleteProductById($id=0)
	{
		try{
			$facadeImage= new Admin_Model_Facade_ImageHandler();
			$dataProd=$this->getProductById($id);
			$res = $this->getAdapter()->delete(TABLE_PREFIX.$this->_name ,"Id=".$id);
			$this->getAdapter()->delete(TABLE_PREFIX.'images_product' ,"Id_Product=".$id);
			$this->getAdapter()->delete(TABLE_PREFIX.'products_attributes' ,"Product_Id=".$id);

			foreach($dataProd['AllImagesByProduct'] as $primages){
				$this->deleteImageProduct($primages['IdImage']);
				$facadeImage->deleteImage($primages['IdImage']);

			}
			return $res;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}



	/**
	 * @param int id
	 * @return boolean
	 */
	public function deleteProductFeaturedById($id=0)
	{
		try{
			$res = $this->getAdapter()->delete(TABLE_PREFIX.'products_featured',"Product_Id=".$id);
			return true;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}





}
